package org.wmq.travelagency02.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.wmq.travelagency02.mapper.ChatGroupMemberMapper;
import org.wmq.travelagency02.model.Entity.ChatGroup;
import org.wmq.travelagency02.model.Entity.ChatGroupMember;
import org.wmq.travelagency02.model.Entity.ChatUser;
import org.wmq.travelagency02.service.ChatGroupMemberService;
import org.wmq.travelagency02.service.ChatGroupService;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ChatGroupMemberServiceImpl extends ServiceImpl<ChatGroupMemberMapper, ChatGroupMember> implements ChatGroupMemberService {
    private final ChatGroupMemberMapper chatGroupMemberMapper;
    @Lazy
    @Autowired
    private ChatGroupService chatGroupService;

    @Override
    public List<ChatUser> getChatUserListByChatGroupId(String chatGroupId) {
        return this.chatGroupMemberMapper.getChatUserListByChatGroupId(chatGroupId);
    }

    @Override
    public boolean removeByChatGroupIdAndChatUserId(String chatGroupId, String chatUserId) {
        ChatGroup chatGroup = this.chatGroupService.lambdaQuery().eq(ChatGroup::getId, chatGroupId)
                .select(ChatGroup::getOwnerId).one();
        if (chatGroup != null && chatGroup.getOwnerId().equals(chatUserId)){
            return false;
        }
        return this.lambdaUpdate().eq(ChatGroupMember::getChatGroupId, chatGroupId)
                .eq(ChatGroupMember::getChatUserId, chatUserId)
                .remove();
    }
}
